# ---------------问题:幂的递归，计算 x 的 n 次方，如：3 的 4 次方 为 333*3=81
# 我的解答，跑题了，但是能算结果
# def mi(x, n):
#     if n == 0:
#         return 1
#     else:
#         return x ** n

# # 正经答案
# def mi(x, n):
#     if n == 0:
#         return 1
#     else:
#         return x * mi(x, n - 1)
#
#
# print(mi(3, 4))

# ---------------问题: 统计列表（list ）中每个元素出现的次数
# lista = [1, 2, 3, 4, 12, 22, 15, 44, 3, 4, 4, 4, 7, 7, 44, 77, 100]
# kong_list = {}
# for i in lista:
#     if i not in kong_list.keys():
#         kong_list[i] = 1
#     else:
#         kong_list[i] += 1
# print(kong_list)

# ---------------问题:[‘abc13’,‘abv89’] 这种列表，打印最大长度的共同的前缀，列表元素个数不确定
# 不会做，给的答案
# def longest_common_prefix(strs):
#     if not strs:
#         return ""
#     print(strs)
#     # 找到最短的字符串,min_str是字符串类型
#     min_str = min(strs, key=len)
#     print(type(min_str))
#
#     # 遍历最短字符串的每个字符
#     for i, char in enumerate(min_str):
#         # 检查所有字符串的当前字符是否相同
#         for other in strs:
#             if other[i] != char:
#                 return min_str[:i]
#
#     # 如果所有字符都匹配，返回最短字符串
#     return min_str
#
#
# # 测试用例
# strs = ['abc13', 'abv89']
# print(longest_common_prefix(strs))  # 输出: 'ab'

# ---------------问题:对 list 去重并找出列表 list 中的重复元素
# 自己写的
# a = [1, 2, 3, 3, 4, 4]
# new_list = []
# for i in a:
#     if i not in new_list:
#         new_list.append(i)
#     else:
#         print("{} is 重复元素".format(i))

# 给的答案
# from collections import Counter
#
# a = [1, 2, 3, 3, 4, 4]
# b = Counter(a)  # b 是一个字典，键是元素，值是元素出现的次数
# print(b)
# print([key for key, value in b.items() if value > 1])
# print({key: value for key, value in b.items() if value > 1})

# ---------------问题:对 list 去重并找出列表 list 中的重复元素
s = "hello"

# 第一种 使用简单 for 循环
# s = list(s)
# s_new = ""
# for i in s:
#     s_new = i + s_new
# print(s_new)

# 第二种 使用原生方法reserved（）
# reversed_s = "".join(reversed(s))
# print(reversed_s)

# # 第三种 使用切片
# print(s[::-1])

# 第四种 使用复杂 for 循环
# s_len = int(len(s))
# new_s = ""
# for i in range(0, s_len):
#     new_s += s[s_len -1 - i]
# print(new_s)

# ---------------问题:两个字符串，怎么找出字符串a在字符串b出现的所有位置和次数，代码实现
# a = "hello"
# b = "helloworld, hello"
# position = []
# start = 0
# while True:
#     index = b.find(a, start)
#     if index == -1:
#         break
#     position.append(index)
#     start = index + 1
# print(position)

# ---------------问题:1,2,3,4 这4个数字，能组成多少个互不相同的且无重复的三位数，都是多少?
# 方法一：自己写的
# new_list = []
# for i in range(1, 5):
#     for j in range(1, 5):
#         for k in range(1, 5):
#             if (i != j) and (j != k) and (i != k):
#                 print(i, j, k)
#                 new_item = i * 100 + j * 10 + k
#                 new_list.append(new_item)
# print(new_list)
# print(len(new_list))
# 方法二: 终极方案，最优解
# import itertools
#
# # 定义数字列表
# digits = [1, 2, 3, 4]
#
# # 生成所有三位数的排列组合
# permutations = itertools.permutations(digits, 3)
# for i in permutations:
#     print(i)
# # 将排列转换为三位数并存储
# numbers = [p[0] * 100 + p[1] * 10 + p[2] for p in permutations]
#
# # 输出结果
# print(f"共有{len(numbers)}个互不相同的三位数，它们是：")
# print(numbers)
# 方法三
# digits = [1, 2, 3, 4]
# result_list = []
# for i in digits:
#     for j in digits:
#         if i == j:
#             continue
#         for k in digits:
#             if i == k or j == k:
#                 continue
#             print(i, j, k)
#             resutl = i * 100 + j * 10 + k
#             result_list.append(resutl)
# print(result_list)

# ---------------问题: 请用 python 打印出 10000 以内的对称数（对称数特点：数字左右对称，如：1,2,11,121,1221 等）
# 方法一：遍历数据
# result_list = []
# for i in range(1, 10000):
#     if str(i)[::-1] == str(i):
#         # print(i)
#         result_list.append(i)
# print(len(result_list))

# 方法二：生成数据
result_list = []
for i in range(1, 10):
    result_list.append(i)
    shiwei = i * 10 + i
    result_list.append(shiwei)
    for j in range(0, 10):
        baiwei = i * 100 + j * 10 + i
        result_list.append(baiwei)
        qianwei = i * 1000 + j * 100 + j * 10 + i
        result_list.append(qianwei)
print(len(result_list))
print(result_list)

# numbers = [num for num in range(1,10000) if str(num) == str(num)[::-1]]
# print(numbers)
# print(len(numbers))














